﻿using Microsoft.AspNetCore.Http;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;

namespace Link_eLab.Helper
{
    public static class ImgHelper
    {
        #region 位图颜色处理类

        /// <summary>
        /// 指定颜色透明化
        /// </summary>
        /// <param name="img">原图</param>
        /// <param name="w">原图宽度</param>
        /// <param name="h">原图高度</param>
        /// <param name="R">指定颜色RGB的R值</param>
        /// <param name="G">指定颜色RGB的G值</param>
        /// <param name="B">指定颜色RGB的B值</param>
        /// <returns>JSON结果集</returns>
        public static Bitmap ConverAlpha(Bitmap img, int w, int h, int R, int G, int B)
        {
            Bitmap bg = new Bitmap(w, h);
            int alpha = 0;
            Color pixel;
            for (int x = 0; x < w; x++)
            {
                for (int y = 0; y < h; y++)
                {
                    pixel = img.GetPixel(x, y);
                    int r1 = pixel.R;
                    int g1 = pixel.G;
                    int b1 = pixel.B;
                    int a = 40;  //色差范围值
                    if (Math.Abs(R - r1) < a && Math.Abs(G - g1) < a && Math.Abs(B - b1) < a)
                    {
                        alpha = 0;    //若两种颜色比较接近，透明度设为0
                    }
                    else
                    {
                        alpha = 255;
                    }
                    bg.SetPixel(x, y, Color.FromArgb(alpha, r1, g1, b1));
                }
            }
            return bg;
        }


        /// <summary>
        /// 指定颜色替换成另一种颜色
        /// </summary>
        /// <param name="img">原图</param>
        /// <param name="w">图宽</param>
        /// <param name="h">图高</param>
        /// <param name="R">要被替换颜色的RGB的R</param>
        /// <param name="G">要被替换颜色的RGB的G</param>
        /// <param name="B">要被替换颜色的RGB的B</param>
        /// <param name="r">替换色的RGB的R</param>
        /// <param name="g">替换色的RGB的G</param>
        /// <param name="b">替换色的RGB的B</param>
        /// <returns>JSON结果集</returns>
        public static Bitmap ImgConver(Bitmap img, int w, int h, int R, int G, int B, int r, int g, int b)
        {
            Bitmap bg = new Bitmap(w, h);
            Color pixel;
            for (int x = 0; x < w; x++)
            {
                for (int y = 0; y < h; y++)
                {
                    pixel = img.GetPixel(x, y);
                    int r1 = pixel.R;
                    int g1 = pixel.G;
                    int b1 = pixel.B;
                    int a = 40;
                    if (Math.Abs(R - r1) < a && Math.Abs(G - g1) < a && Math.Abs(B - b1) < a)
                    {
                        bg.SetPixel(x, y, Color.FromArgb(r, g, b));
                    }
                    else
                    {
                        bg.SetPixel(x, y, Color.FromArgb(r1, g1, b1));
                    }

                }
            }
            return bg;
        }

        #endregion
    }
}
